package com.silong.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Calendar;
import java.util.UUID;

public class Tools {
	static Log logger = LogFactory.getLog(Tools.class);
	
	/**
	 * 生成UUID
	 * @return
	 */
	public static String createUUID(){
		return UUID.randomUUID().toString().replace("-", "").toUpperCase();
	}
	
	/**
	 * 身份证号码验证
	 * 
	 * @param idCardNo
	 * @return
	 */
	public static boolean isIdCardNo(String idCardNo) {
		String[] powers = { "7", "9", "10", "5", "8", "4", "2", "1", "6", "3",
				"7", "9", "10", "5", "8", "4", "2" };
		String[] parityBits = { "1", "0", "X", "9", "8", "7", "6", "5", "4",
				"3", "2" };
		if (idCardNo.length() != 18 && idCardNo.length() != 15) {
			return false;
		}

		if (idCardNo.length() == 18) {
			String num = idCardNo.substring(0, 17);
			String parityBit = idCardNo.substring(17);
			int power = 0;
			for (int i = 0; i < 17; i++) {
				// 校验每一位的合法性
				if (num.charAt(i) < '0' || num.charAt(i) > '9') {
					return false;
				} else {
					// 加权
					power += Integer.parseInt(String.valueOf(num.charAt(i)))
							* Integer.parseInt(powers[i]);
				}
			}
			// 取模
			int mod = power % 11;
			if (parityBits[mod].equals(parityBit)) {
				return true;
			} else {
				return false;
			}
		} else {
			// 校验15位的身份证号码
			for (int i = 0; i < idCardNo.length(); i++) {
				// 校验每一位的合法性
				if (idCardNo.charAt(i) < '0' || idCardNo.charAt(i) > '9') {
					return false;
				}
			}
			int year = Integer.parseInt(idCardNo.substring(6, 8));
			int month = Integer.parseInt(idCardNo.substring(8, 10));
			int date = Integer.parseInt(idCardNo.substring(10, 12));
			Calendar calendar = Calendar.getInstance();
			calendar.set(year, month - 1, date);
			// 日期合法性
			if (calendar.get(Calendar.YEAR) != year
					|| calendar.get(Calendar.MONTH) + 1 != month
					|| calendar.get(Calendar.DATE) != date) {
				return false;
			}
			return true;
		}
	}


	public static String createNo(){
		return DateUtil.formatDateToString(Calendar.getInstance().getTime(), DateUtil.SIX_FORMAT);
	}

	public static void main(String[] s){
		System.out.println(createNo());
	}

}
